//
//  CreateJobReviewSystemDetailView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/7.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
class CreateJobReviewSystemDetailView: View {
    fileprivate lazy var disposeBag = DisposeBag()
    let titleView = CreateJobReviewSectionView(R.string.localizable.systemDetails().uppercased(),haveEditBtnFlag: true)
    //
    let connectTypeView = CreateJobReviewConnectTypeView()
    //
    let instalationTypeText = CreateJobReviewContentLabel()
    //
    let panelTableView = UITableView()
    let inverterTableView = UITableView()
    let batteryTableView = UITableView()
    //contraint
    var panelTableViewHeightConstraint: Constraint?
    var inverterTableViewHeightConstraint: Constraint?
    var batteryTableViewHeightConstraint: Constraint?
    //
    let optimizerInstalledText = CreateJobReviewContentLabel()
    override func setUpView() {
        //titleView
        self.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        //Connect type view
        self.addSubview(connectTypeView)
        connectTypeView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom).offset(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.size.greaterThanOrEqualTo(0)
        }
        //connectTypeSeperateLineView
        let connectTypeSeperateLineView = View(hexStr: "#EDEDED")
        self.addSubview(connectTypeSeperateLineView)
        connectTypeSeperateLineView.snp.makeConstraints { make in
            make.top.equalTo(connectTypeView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.right.equalTo(connectTypeView)
            make.size.equalTo(1)
        }
        //instalation type
        let instalationTypeTitle = CreateJobReviewTitleLabel(R.string.localizable.installationType())
        self.addSubview(instalationTypeTitle)
        instalationTypeTitle.snp.makeConstraints { make in
            make.top.equalTo(connectTypeSeperateLineView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.equalTo(connectTypeView)
            make.size.greaterThanOrEqualTo(0)
        }
        //
        instalationTypeText.numberOfLines = 0
        instalationTypeText.textAlignment = .right
        self.addSubview(instalationTypeText)
        instalationTypeText.snp.makeConstraints { make in
            make.centerY.equalTo(instalationTypeTitle)
            make.right.equalTo(connectTypeView)
            make.size.greaterThanOrEqualTo(0)
        }
        //panel tableview cell
        self.addSubview(panelTableView)
        panelTableView.snp.makeConstraints { make in
            make.top.equalTo(instalationTypeTitle.snp.bottom).offset(16)
            make.left.right.equalTo(connectTypeView)
            self.panelTableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
        }
        //inverter tableview cell
        self.addSubview(inverterTableView)
        inverterTableView.snp.makeConstraints { make in
            make.top.equalTo(panelTableView.snp.bottom)
            make.left.right.equalTo(panelTableView)
            self.inverterTableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
        }
        //battery tableview cell
        self.addSubview(batteryTableView)
        batteryTableView.snp.makeConstraints { make in
            make.top.equalTo(inverterTableView.snp.bottom)
            make.left.right.equalTo(panelTableView)
            self.batteryTableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
        }
        //optimizerInstalledLineView
        let optimizerInstalledLineView = View(hexStr: "#EDEDED")
        self.addSubview(optimizerInstalledLineView)
        optimizerInstalledLineView.snp.makeConstraints { make in
            make.top.equalTo(batteryTableView.snp.bottom)
            make.left.right.equalTo(connectTypeView)
            make.size.equalTo(1)
        }
        //optimizer installer
        let optimizerInstalledTitle = CreateJobReviewTitleLabel(R.string.localizable.willThereBeAnyOptimisersInstalled())
        self.addSubview(optimizerInstalledTitle)
        optimizerInstalledTitle.snp.makeConstraints { make in
            make.top.equalTo(optimizerInstalledLineView.snp.bottom).offset(CREATEJOB_REVIEW_INTERNAL)
            make.left.equalTo(connectTypeView)
            make.size.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-16)
        }
        self.addSubview(optimizerInstalledText)
        optimizerInstalledText.snp.makeConstraints { make in
            make.centerY.equalTo(optimizerInstalledTitle)
            make.right.equalTo(connectTypeView)
            make.size.greaterThanOrEqualTo(0)
        }
        
        //
        panelTableView.separatorStyle = .none
        inverterTableView.separatorStyle = .none
        batteryTableView.separatorStyle = .none
        panelTableView.register(JobDetailPanelTableViewCell.self, forCellReuseIdentifier: JobDetailPanelTableViewCell.reuseCellIdentifier)
        inverterTableView.register(JobDetailInverterTableViewCell.self, forCellReuseIdentifier: JobDetailInverterTableViewCell.reuseCellIdentifier)
        batteryTableView.register(JobDetailBatteryTableViewCell.self, forCellReuseIdentifier: JobDetailBatteryTableViewCell.reuseCellIdentifier)
        
        //监听属性变化
        panelTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.panelTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //监听属性变化
        inverterTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.inverterTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //监听属性变化
        batteryTableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.batteryTableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //
        self.bindData()
    }
    private func bindData(){
        //设置cell信息，panels绑定数据源
        CreateJobModelHelper.shared.systemInfoObserver.map({$0.panels}).bind(to: panelTableView.rx.items(cellIdentifier: JobDetailPanelTableViewCell.reuseCellIdentifier,cellType: JobDetailPanelTableViewCell.self)){(row, model, cell) in
            cell.setData(model,index: row)
        }.disposed(by: self.disposeBag)
        //设置cell信息，inverters绑定数据源
        CreateJobModelHelper.shared.systemInfoObserver.map({$0.inverters}).bind(to: inverterTableView.rx.items(cellIdentifier: JobDetailInverterTableViewCell.reuseCellIdentifier,cellType: JobDetailInverterTableViewCell.self)){(row, model, cell) in
            cell.setData(model,index: row)
        }.disposed(by: self.disposeBag)
        //设置cell信息，batterys绑定数据源
        CreateJobModelHelper.shared.systemInfoObserver.map({$0.batteries}).bind(to: batteryTableView.rx.items(cellIdentifier: JobDetailBatteryTableViewCell.reuseCellIdentifier,cellType: JobDetailBatteryTableViewCell.self)){(row, model, cell) in
            cell.setData(model,index: row)
        }.disposed(by: self.disposeBag)
        //
        CreateJobModelHelper.shared.systemInfoObserver.map({$0.connected_type}).subscribe { [weak self] gridConnectedValue in
            guard let strongSelf = self else {return }
            strongSelf.connectTypeView.setData()
        }.disposed(by: self.disposeBag)
    }
    func setData(){
        self.instalationTypeText.text = CreateJobModelHelper.shared.createJobViewModel.systemDetail.installation_type_name
        self.connectTypeView.setData()
        self.optimizerInstalledText.text = (CreateJobModelHelper.shared.createJobViewModel.systemDetail.installed_optimiser ? "Yes" : "No")
    }
    
}
